package br.com.sgdc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import br.com.sgdc.view.action.ActionAutenticarButtonWindowsCadastroClouds;
import br.com.sgdc.view.action.ActionDeleteCloudButtonWindowCadastroClouds;
import br.com.sgdc.view.action.ActionNomeCloudLabelWindowCadastroClouds;
import br.com.sgdc.view.action.ActionWindowCadastroClouds;
import br.com.sgdc.view.ModeloTabelaWindowCadastroClouds;
import br.com.sgdc.view.util.Imagens;
import br.com.sgdc.view.util.TipoCloud;

public class WindowCadastroClouds {
	public JDialog window = new JDialog();
	
	public JLabel cloudLabel = new JLabel("Cloud: ");
	public JComboBox<String> tipoCloudComboBox = new JComboBox<String>();
	
	public JLabel nomeCloudLabel = new JLabel("Nome Cloud: ");
	public JTextField nomeCloudTextField = new JTextField(15);
	
	public JButton confirmarButton = new JButton("Confirmar");
	public JButton deleteCloudButton = new JButton("Excluir Cloud");
	
	private ModeloTabelaWindowCadastroClouds modelo = new ModeloTabelaWindowCadastroClouds();
	
	public ModeloTabelaWindowCadastroClouds getModelo() {
		return modelo;
	}

	public WindowCadastroClouds() {
		window.setTitle(".:: SGDC - Clouds ::.");
		window.setLayout(new BorderLayout());
		window.setSize(450, 400);
		window.setLocationRelativeTo(null);
		window.setIconImage(new ImageIcon(getClass().getResource(Imagens.cloud)).getImage());
	}

	public void initComponentesJanela() {
		this.initComponentes();	

		window.setLayout(new BorderLayout());
		window.add(BorderLayout.NORTH,constrcutPanelNorth());
		window.add(BorderLayout.CENTER,constructTblCloud());
		window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		window.setVisible(true);
	}
	
	private void initComponentes() {
		confirmarButton.setIcon(new ImageIcon(getClass().getResource(Imagens.ok)));
		deleteCloudButton.setIcon(new ImageIcon(getClass().getResource(Imagens.delete)));
		
		this.constructCampoComboBox();
		this.contructAction();
	}

	private void constructCampoComboBox() {
		tipoCloudComboBox.addItem("");
		tipoCloudComboBox.addItem(TipoCloud.DROPBOX.toString());
		tipoCloudComboBox.addItem(TipoCloud.GOOGLE_DRIVE.toString());
		tipoCloudComboBox.setSelectedIndex(0);
	}

	private JPanel constrcutPanelNorth() {
		JPanel principalPanel = new JPanel();
		principalPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		principalPanel.setBorder(BorderFactory.createTitledBorder("Cadastro de Clouds"));
		principalPanel.add(constructCadastroPanel());
		
		JPanel panelAux = new JPanel();
		panelAux.setLayout(new BorderLayout());
		panelAux.setBorder(BorderFactory.createRaisedBevelBorder());
		panelAux.add(BorderLayout.CENTER,principalPanel);
		panelAux.add(BorderLayout.SOUTH,constructPanelSouth());

		return panelAux;
	}
	
	private JPanel constructPanelSouth() {
		JPanel panelSouth = new JPanel();
		panelSouth.setLayout(new FlowLayout(FlowLayout.LEFT));
		panelSouth.add(deleteCloudButton);
		
		return panelSouth;
	}

	private JPanel constructCadastroPanel() {		
		JPanel panelGrid = new JPanel();
		panelGrid.setLayout(new GridLayout(3,2));

		panelGrid.add(nomeCloudLabel);
		panelGrid.add(nomeCloudTextField);
		
		panelGrid.add(cloudLabel);
		panelGrid.add(tipoCloudComboBox);
		
		panelGrid.add(new JLabel());
		panelGrid.add(confirmarButton);


		JPanel principalPanel = new JPanel();
		principalPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		principalPanel.add(panelGrid);

		return principalPanel;
	}

	private JScrollPane constructTblCloud() {
		JTable tabela = new JTable(modelo);
		tabela.setPreferredScrollableViewportSize(new Dimension(300,150));

		JScrollPane scrollPane = new JScrollPane(tabela);
		scrollPane.setBackground(Color.DARK_GRAY);

		return scrollPane;
	}

	private void contructAction() {
		window.addWindowListener(new ActionWindowCadastroClouds(this));
		confirmarButton.addActionListener(new ActionAutenticarButtonWindowsCadastroClouds(this));
		deleteCloudButton.addActionListener(new ActionDeleteCloudButtonWindowCadastroClouds(this));
		nomeCloudLabel.addMouseListener(new ActionNomeCloudLabelWindowCadastroClouds(this));
	}
}